package leetco.test;

/**
 * 合并两个链表
 */
public class MergeNode extends ListNode {

    public static void main(String[] args) {
        ListNode node1 = createNode(1, 2, 4, 6, 9, 10);
        ListNode node2 = createNode(3, 5, 7, 8, 11, 12);
        System.out.println("node1: " + node1);
        System.out.println("node2: " + node2);

        System.out.println("after merge: " + merge(node1, node2));
    }


    private static ListNode merge(ListNode node1, ListNode node2) {
        if (node1 == null) {
            return node2;
        }
        if (node2 == null) {
            return node1;
        }
        if (node1.val < node2.val) {
            node1.next = merge(node1.next, node2);
            return node1;
        } else {
            node2.next = merge(node1, node2.next);
            return node2;
        }
    }

}
